package com.atguigu.yygh.user.controller.user;


import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.common.util.AuthContextHolder;
import com.atguigu.yygh.common.util.JwtHelper;
import com.atguigu.yygh.enums.AuthStatusEnum;
import com.atguigu.yygh.model.user.UserInfo;
import com.atguigu.yygh.user.service.UserInfoService;
import com.atguigu.yygh.vo.user.LoginVo;
import com.atguigu.yygh.vo.user.UserAuthVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2023-06-01
 */
@RestController
@RequestMapping("/user/userinfo")
@Api(value = "用户模块")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;


    @PostMapping("/login")
    @ApiOperation(value = "登录操作")
    public R login(@RequestBody LoginVo loginVo) {
        Map<String, Object> info = userInfoService.login(loginVo);
        return R.ok().data(info);
    }

    @GetMapping("/auth/getUserInfo")
    @ApiOperation(value = "查询用户基本信息")
    public R getUserInfo(@RequestHeader String token) {
        Long userId = JwtHelper.getUserId(token);
        UserInfo info = userInfoService.getById(userId);
        info.getParam().put("authStatusString", AuthStatusEnum.getStatusNameByStatus(info.getAuthStatus()));
        return R.ok().data("userInfo", info);
    }

    @PutMapping("/auth/userAuth")
    @ApiOperation(value = "保存用户信息")
    public R userAuth(@RequestHeader("token") String token,
                      @RequestBody UserAuthVo userAuthVo) {
        System.out.println("token = " + token);
        Long userId = JwtHelper.getUserId(token);
        UserInfo userInfo = new UserInfo();
        userInfo.setId(userId);
        userInfo.setName(userAuthVo.getName());
        userInfo.setCertificatesType(userAuthVo.getCertificatesType());
        userInfo.setCertificatesNo(userAuthVo.getCertificatesNo());
        userInfo.setCertificatesUrl(userAuthVo.getCertificatesUrl());
        userInfo.setAuthStatus(AuthStatusEnum.AUTH_RUN.getStatus());
        userInfoService.updateById(userInfo);
        return R.ok();
    }

}

